dofile("config.lua")
dofile("convert-js.lua")
dofile("get-all-js.lua")
dofile("../Talented_GatherData.lua")

local db = Talented_GatherDB and Talented_GatherDB[Talented_GatherDB.latest]
Talented_GatherDB = nil

local LANG = "en"
local BUILD = "3.0.1 - 8714 - Aug  1 2008 - 30000"

local data = {}

local whd = {}

local function download_wh(class)
	local file = "wowhead-"..class..".html"
	local content = get_content(file)
	if content then return content end
	download("http://wotlk.wowhead.com/?spells=-2."..CLASSES[class], file)
	return get_content(file)
end

local function get_wh_info(content)
	local info = {}
	local icons = {}
	local spells = {}

	for id, icon in content:gmatch("_%[([0-9]+)%]={icon:'([^']+)'}") do
		icons[tonumber(id)] = icon
	end
	for id, name in content:gmatch("{id:([0-9]+),name:'@(.-)',") do
		spells[tonumber(id)] = name:replace("\\", "")
	end
	for id, spell in pairs(spells) do
		icon = icons[id]
		info[spell] = "Interface\\Icons\\"..icon
	end
	return info
end

local SPELLMAP = {
	["Beast Mastery"] = "Beast Mastery [NYI]",
	["Improved Tracking"] = "Improved Tracking [NYI]",
	["Improved Rend"] = "Bloodletting",
	["Focus Mind"] = "Focus Magic",
	["Cold as Ice"] = "Ice Floes",
	["Fire and Brimstone"] = "Eternal Flames",
--	["Tidal Force"] = "",
}

local function wh_get_icon(class, spell)
	local data = whd[class]
	if not data then
		data = get_wh_info(download_wh(class))
		whd[class] = data
	end
	return data[spell] or data[spell.." [NYI]"]
end

for class in pairs(CLASSES) do
	local t = conv_data(class, LANG)
	local tree = {}
	data[class] = tree
	for i, name in ipairs(t.tree) do
		local branch = {
			info = {
				icon = db[class] and db[class][i].info.icon or "", -- don't have this, sadly
				background = db[class] and db[class][i].info.background or "", -- don't have this also
			},
			talents = {
			},
		}
		tree[i] = branch
		for j, spell in ipairs(t.talent) do
			if spell[1] == (i - 1) then
				local icon = wh_get_icon(class, spell[2])
				if not icon then
					print(string.format("Icon not found : %s %d %d %s", class, i, j, spell[2]))
				end
				branch.talents[#branch.talents + 1] = {
					info = {
						column = spell[4],
						icon = icon or "",
						row = spell[5],
						ranks = spell[3],
					}
				}
			end
		end
	end
end

local function tokey(v)
	if type(v) == "string" and v:find(".", 1, 1) then
		return string.format("[%q]", v)
	elseif type(v) == "number" then
		return string.format("[%s]", v)
	else
		return v
	end
end

local function dump(o, data, name, offset)
	offset = offset or 0
	name = name or "Talented_GatherDB"

	local tab = string.rep("\t", offset)
	o:write(string.format("%s%s = ", tab, tokey(name)))
	if type(data) == "table" then
		o:write("{\n")
		for k, v in pairs(data) do
			dump(o, v, k, offset + 1)
			o:write(",\n")
		end
		o:write(string.format("%s}", tab))
	elseif type(data) == "string" then
		o:write(string.format("%q", data))
	elseif type(data) == "number" then
		o:write(data)
	end
end

f = io.open("Talented_GatherData.lua", "w")
dump(f, { latest = BUILD, [BUILD] = data})
